﻿Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.IO

Public Class Form2
    Dim tenFileSuDung As String = ""
    Dim tenFileGrid As String = ""
    Dim dirTemp As String = ""
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDanhSach.CellContentClick
        
    End Sub

   

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'ob_cnn.Open()
        Dim cmd As New SqlCommand("select id , luuNoidung from Luufile", ob_cnn)
        Dim dt As New DataTable()

        dt.Load(cmd.ExecuteReader())
        '   ob_cnn.Close()
        dgvDanhSach.DataSource = dt



    End Sub

    Private Sub dgvDanhSach_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDanhSach.CellClick
        Try

            Dim id As String
            tenFileGrid = dgvDanhSach.Rows(e.RowIndex).Cells("luunoidung").Value.ToString()
            id = dgvDanhSach.Rows(e.RowIndex).Cells("id").Value.ToString()
            dirTemp = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            Dim img As Byte() = RetrieveImage1(tenFileGrid, Integer.Parse(id))
            If Not Directory.Exists(dirTemp & "\\Quan Ly Kho") Then
                Directory.CreateDirectory(dirTemp & "\\Quan Ly Kho")
            End If
            tenFileSuDung = dirTemp & "\\Quan Ly Kho\\" & tenFileGrid.Split("\\").Last()



            File.WriteAllBytes(tenFileSuDung, img)
            AxAcroPDF1.src = tenFileSuDung
            'Dim proc As New ProcessStartInfo()


            'proc.WindowStyle = ProcessWindowStyle.Normal
            'proc.FileName = tenFileSuDung
            'prcss = Process.Start(proc)

        Catch ex As Exception
            'If ex.Message.Contains("because it is being used by another process") Then
            '    MessageBox.Show("File bạn đang mở !")
            'End If
        End Try
    End Sub

    Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        XoafileTam(dirTemp)
    End Sub

    Private Sub dgvDanhSach_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDanhSach.CellContentDoubleClick

    End Sub
End Class